<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\BizException;
use App\Http\Controllers\Controller;
use App\Model\Agent;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class AgentController extends Controller
{
    public function __construct()
    {
        $this->middleware(['admin']);
    }

    public function index(Request $request)
    {
        $params = $request->all();
        $model = Agent::where('status', '>', '-1');
        !empty($params['username']) && $model->where('username', 'like', "%{$params['username']}%");
        !empty($params['name']) && $model->where('name', 'like', "%{$params['name']}%");
        $list = $model->orderBy('id', 'desc')->paginate(15);
        return view('admin.agent.index', [
            'list' => $list,
            'params' => $params,
            'hostName' => $request->getHost(),
        ]);
    }

    public function add(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'username' => 'required|unique:agent',
                    'password' => 'required',
                    'name' => 'required',
                ], [
                    'username.required' => '用户名必填',
                    'username.unique' => '该用户名已存在',
                    'password.required' => '密码必填',
                    'name.required' => '公司名必填',
                ]);

                $params = $request->all();
                if (!preg_match('/^\w{6,}$/', $params['password']))
                    throw new BizException('密码至少是6位的字母或数字组合');

                $params['password'] = bcrypt($params['password']);
                $params['created_at'] = date('Y-m-d H:i:s');
                $params['updated_at'] = date('Y-m-d H:i:s');
                Agent::insert($params);
                return response()->json(['status' => 1, 'info' => '保存成功']);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
        } else {
            return view('admin.agent.add');
        }
    }

    public function edit(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'name' => 'required',
                ], [
                    'name.required' => '公司名必填',
                ]);

                $params = $request->all();
                if (isset($params['id']))
                    unset($params['id']);
                Agent::where('id', $request->input('id'))->update($params);
                return response()->json(['status' => 1, 'info' => '保存成功']);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
        } else {
            return view('admin.agent.edit', [
                'row' => Agent::find($request->input('id')),
            ]);
        }
    }

    public function del(Request $request)
    {
        Agent::where('id', $request->input('id'))->update(['status' => -1]);
        return response()->json(['status' => 1, 'info' => '删除成功']);
    }

    public function resetPwd(Request $request)
    {
        Agent::where('id', $request->input('id'))->update(['password' => bcrypt('123456')]);
        return response()->json(['status' => 1, 'info' => '重置成功']);
    }
}